package com.github.ecbp.common.security.utils;


import com.alibaba.fastjson.JSON;
import com.github.ecbp.common.constant.CacheKeyConstant;
import com.github.ecbp.common.security.vo.DefaultSimpleGrantedAuthority;
import com.github.ecbp.common.security.vo.DefaultUserDetails;
import com.github.ecbp.common.utils.SpringUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

public class CommonAdminUtils {

    private static JwtTokenUtil jwtTokenUtil = null;

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return Optional.ofNullable(attributes).map(ServletRequestAttributes::getRequest).orElse(null);
    }

    /**
     * 获取请求头中的 token
     *
     * @return
     */
    public static String getTokenFromHeader() {
        if (null == jwtTokenUtil) {
            jwtTokenUtil = SpringUtils.getBean(JwtTokenUtil.class);
        }

        String token = Optional.ofNullable(getRequest()).map(req -> req.getHeader(jwtTokenUtil.getTokenHeader())).orElse(null);
        return StringUtils.isEmpty(token) ? CacheKeyConstant.UNKNOWN : token;
    }

    /**
     * 直接获取当前登录用户名
     *
     * @return
     */
    public static String getLoginPhoneFromToken() {
        String token = getTokenFromHeader();
        if (!CacheKeyConstant.UNKNOWN.equals(token)) {
            String phone = jwtTokenUtil.getLoginPhoneFromToken(token);
            return StringUtils.isEmpty(phone) ? CacheKeyConstant.UNKNOWN : phone;
        }
        return CacheKeyConstant.UNKNOWN;
    }

    /**
     * 直接获取当前登录用户id
     *
     * @return
     */
    public static Long getLoginIdFromSystem() {
        DefaultUserDetails userDetails =  getUserDetailsFromSystem();
        return null == userDetails ? -1L : userDetails.getId();
    }

    /**
     * 直接获取当前登录用户icon
     *
     * @return
     */
    public static String getIconFromSystem() {
        DefaultUserDetails userDetails = getUserDetailsFromSystem();
        return null == userDetails ? CacheKeyConstant.UNKNOWN : userDetails.getIcon();
    }

    /**
     * 直接获取当前登录用户名
     *
     * @return
     */
    public static String getUsernameFromSystem() {
        DefaultUserDetails userDetails = getUserDetailsFromSystem();
        return null == userDetails ? CacheKeyConstant.UNKNOWN : userDetails.getLoginName();
    }

    /**
     * 直接获取当前登录用户信息
     *
     * @return
     */
    public static DefaultUserDetails getUserDetailsFromSystem() {
        String loginPhone = getLoginPhoneFromToken();
        if (!StringUtils.isEmpty(loginPhone)) {
            return getUserDetailsByLoginPhone(loginPhone, DefaultUserDetails.class);
        }
        return null;
    }

    /**
     * 直接通过用户名获取当前登录用户信息
     *
     * @param loginPhone
     * @return
     */
    public static <T extends DefaultUserDetails> T getUserDetailsByLoginPhone(String loginPhone, Class<T> classType) {
        if (null == loginPhone) {
            return null;
        }

        if (null == jwtTokenUtil) {
            jwtTokenUtil = SpringUtils.getBean(JwtTokenUtil.class);
        }

        String result = jwtTokenUtil.getUserInfo(loginPhone);
        if (null != result) {
            T userDetails = JSON.parseObject(result, classType);
            List<DefaultSimpleGrantedAuthority> grantedAuthorities = JSON.parseArray(JSON.parseObject(result).getString(
                    "grantedAuthorities"), DefaultSimpleGrantedAuthority.class);
            userDetails.setGrantedAuthorities(grantedAuthorities);
            return userDetails;
        }

        return null;
    }
}
